<template>
    <div class="star">
        <button @click="addStar"
                v-show="this.state<3"
                :class="{'unconcerned':this.state === 0,
        'concerned':this.state ===1 }">{{starText}}
        </button>
    </div>
</template>

<script>
    import {isLogin} from '@/auth/authService';

    export default {
        name: 'star',
        data() {
            return {
                state: 3,
                starText: '关注'
            };
        },
        props: ['targetId'],
        mounted() {
            if (this.targetId) { // 目标作者ID 如果父组件没有传递稍等
                if (isLogin() && !this.CONF.user) {
                    this.$Utils.login.getUserInfo().then((loginInfo) => {
                        if (loginInfo) {
                            this.CONF.user = loginInfo.user;
                            this.starCheck(this.targetId, this.CONF.user);
                        }
                    }).catch((error) => {
                        this.$Modal.error({content: error.body});
                    });
                } else if (this.CONF.user) {
                    this.starCheck(this.targetId, this.CONF.user);
                }
            }
        },
        methods: {
            starCheck(targetId, user) {
                if (targetId !== user.id) {
                    const flag = user.starsList.find(item => item === targetId);
                    if (flag) {
                        this.state = 1;
                        this.starText = '已关注';
                    } else {
                        this.state = 0;
                        this.starText = '关注';
                    }
                } else {
                    this.state = 3;
                }
            },
            addStar() {
                if (!this.CONF.user) {
                    this.$Modal.warning({
                        title: '还未登入~',
                        content: '是否前去登入?',
                        onCancel: () => {
                        },
                        onOk: () => {
                            this.$router.push({
                                name: 'login', query: {type: 'login'}
                            });
                        }
                    });
                } else if (this.state === 0) {
                    this.$Api.addStar({
                        sourceId: this.CONF.user.id,
                        targetId: this.targetId
                    }).then((res) => {
                        if (res.ok) {
                            return this.$Utils.login.getUserInfo().then((loginInfo) => {
                                if (loginInfo) {
                                    this.CONF.user = loginInfo.user;
                                    this.starCheck(this.targetId, this.CONF.user);
                                }
                            });
                        }
                    }).catch((error) => {
                        this.$Modal.error({content: error.body});
                    });
                }
            }
        },
        watch: {
            targetId(value) {
                if (isLogin() && !this.CONF.user) {
                    this.$Utils.login.getUserInfo().then((loginInfo) => {
                        if (loginInfo) {
                            this.CONF.user = loginInfo.user;
                            this.starCheck(value, this.CONF.user);
                        }
                    }).catch((error) => {
                        this.$Modal.error({content: error.body});
                    });
                } else if (this.CONF.user) {
                    this.starCheck(value, this.CONF.user);
                }
            }
        }
    };
</script>

<style lang="scss">
    .star {
        button {
            height: 30px !important;
            padding: 0 10px !important;
            font-size: 15px !important;
            line-height: 30px;
            text-align: center;
            border-radius: 10px !important;
            box-shadow: none;

        }
        .unconcerned {
            background: rgb(253, 163, 62);
            color: white;
            border: none;
        }
        .concerned {
            background: #ccc;
            color: white;
            border: none;
        }
    }
</style>
